package com.bike.controller.information;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bike.controller.BaseController;
import com.bike.entity.HhDining;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhDiningServiceImpl;
import com.bike.service.impl.HhDiningStyleServiceImpl;
import com.bike.util.Constant;
import com.bike.util.FileZip;
import com.bike.util.PageData;
import com.bike.util.Tools;

 
@Controller
@RequestMapping(value="/hhDining")
public class HhDiningController extends BaseController {
	
		@Autowired
		private HhDiningServiceImpl  hhDiningService;
		@Autowired
		private HhBusinessServiceImpl  hhBusinessServiceImpl;
		@Autowired
		private HhDiningStyleServiceImpl  hhDiningStyleServiceImpl;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/information/dining/hhDining_list";
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhDiningService.selectHhDiningList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhDiningService.selectHhDiningList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd",pd);
			if(pd.getLong("diningId")>0){
				model.addAttribute("pd", hhDiningService.selectByHhDiningId(pd.getLong("diningId")));
			} 
			return "/information/dining/hhDining_edit";
		}
		/**
		 * 搜素
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("toSearch")
		public String toSearch(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd", pd);
			return "/information/dining/hhDining_search";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,HhDining hhDining){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				hhDiningService.saveOrUpdate(request,pd,hhDining);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("diningId")>0){
					hhDiningService.deleteHhDiningById(pd.getLong("diningId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
	 	
	 	/**
	 	 * 桌牌打包下载
	 	 * @param request
	 	 * @param response
	 	 */
	 		 @RequestMapping(value = "/zipDining")
	 	    public void zipDining(HttpServletRequest request,HttpServletResponse response) {
	 			 
	 		 	try {
	 			 		PageData pd =PageData.getToPageData(request);
	 			       if(Tools.notEmpty(pd.getString("businessId"))){
	 						List<HhDining> list = hhDiningService.listAll(pd);
	 						List<InputStream> iss=new ArrayList<>();
	 						 
	 						for(HhDining pdList:list){
	 							iss.add( new FileInputStream(new File(Constant.filePath.concat(pdList.getImg()))));
	 						}
	 						FileZip.zipAndDownload("dining.zip", response, iss,".".concat(Constant.format));
	 			       }
	 				} catch (Exception e) {
	 					e.printStackTrace();
	 				}
	 	    }
}
